package com.example.myapplication;

import static com.example.myapplication.master.AtomicIntegerDemo1.atomicInteger;
import static com.example.myapplication.master.AtomicIntegerDemo1.atomicInteger2;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;

public class master {

    public static void main(String[] args) throws InterruptedException {

//        AtomicIntegerDemo1 aid = new AtomicIntegerDemo1();
//        Thread thread1 = new Thread(aid);
//        Thread thread2 = new Thread(aid);
//        thread1.start();
//        thread2.start();
//        thread1.join();
//        thread2.join();
//
//        System.out.println("原子类的结果：" + atomicInteger.get());
//        System.out.println("普通变量值：" + AtomicIntegerDemo1.basicCount);
//        System.out.println("getAndAdd的结果：" + atomicInteger2.get());

        AtomicInteger atomicInteger = new AtomicInteger();

        AtomicBoolean atomicBoolean = new AtomicBoolean();

        int i = 0;

        atomicBoolean.set(true);

        System.out.println(i++);
        System.out.println(++i);
        System.out.println(atomicInteger.get());
        System.out.println(atomicInteger.getAndAdd(4));
        System.out.println(atomicInteger.addAndGet(4));
        System.out.println(atomicInteger.getAndIncrement());
        System.out.println(atomicInteger.incrementAndGet());
        AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(1000);
        System.out.println(atomicIntegerArray.getAndIncrement(0));
        System.out.println(atomicIntegerArray.incrementAndGet(0));

    }

    public static class AtomicIntegerDemo1 implements Runnable {

        static volatile int basicCount = 0;

        static final AtomicInteger atomicInteger = new AtomicInteger();
        static final AtomicInteger atomicInteger2 = new AtomicInteger();

        //原子类型自增
        public void atomicIncrement() {
            atomicInteger.getAndIncrement();
        }

        //原子类型 getAndAdd()
        public void atomicAdd() {
            atomicInteger2.getAndAdd(4);
        }

        //普通类型自增
        public void basicIncrement() {
            basicCount++;
        }


        @Override
        public void run() {
            for (int i = 0; i < 10000; i++) {
                atomicIncrement();
                basicIncrement();
                atomicAdd();
            }
        }
    }
}
